这段代码是libHaru库的一部分，专门用于处理TrueType字体（TTF）的加载和嵌入到PDF文档中。libHaru是一个用于生成PDF文件的免费、跨平台的开源库，它允许用户在PDF中嵌入TrueType字体，而无需了解PDF内部结构的复杂性。

以下是代码中定义的关键函数及其功能概述：

1. **FreeFunc**：当字体定义对象被销毁时调用此函数，用于释放与字体定义相关的所有资源。

2. **LoadFontData**：加载TrueType字体数据到内存中，并解析字体的关键信息，如头部信息、最大轮廓点数、字形映射等。

3. **LoadFontData2**：从TrueType集合字体（TTC文件）中加载特定索引的字体数据。

4. **InitAttr**：初始化字体定义属性，包括字符集、水平度量、名称表等。

5. **GetUINT32/16/INT16**：从输入流中读取无符号32位、16位整数和有符号16位整数，并对它们进行字节序转换。

6. **WriteUINT32/16/INT16**：将无符号32位、16位整数和有符号16位整数写入输出流，并进行字节序转换。

7. **UINT32Swap/UINT16Swap/INT16Swap**：对32位无符号整数、16位无符号整数和16位有符号整数进行字节序转换。

8. **LoadTTFTable**：加载TrueType字体的表目录。

9. **ParseHead/Maxp/Hhea/CMap/Hmtx/Loca/Name/OS2**：解析TrueType字体的不同表，如头部信息、最大点数、水平头信息、字符映射表、水平度量表、位置表、名称表和操作系统/2表。

10. **FindTable**：根据标签名查找TrueType字体表。

11. **CheckCompositGryph**：检查复合字形，用于确定是否需要嵌入字形。

12. **HPDF_TTFontDef_New/Load**：创建新的TrueType字体定义对象并加载字体数据。

13. **HPDF_TTFontDef_GetGlyphid/GetCharWidth**：获取字符对应的字形ID和字符宽度。

14. **HPDF_TTFontDef_GetGidWidth**：根据字形ID获取字形宽度。

15. **ParseHmtx**：解析水平度量表，用于获取字符的宽度信息。

16. **ParseLoca**：解析位置表，用于确定字形数据的起始位置。

17. **LoadUnicodeName**：从字体中加载Unicode名称。

18. **RecreateGLYF/RecreateName**：重新创建GLYF（字形）表和名称表，用于嵌入字体时存储修改后的表。

19. **WriteHeader**：写入TrueType字体的头部信息。

20. **HPDF_TTFontDef_SaveFontData**：保存字体数据到PDF文档中，包括重新计算字体的校验和等。

21. **HPDF_TTFontDef_SetTagName**：设置字体的标签名，用于PDF文档中引用字体。

代码中还包含了一些辅助函数，如内存分配、错误处理和流操作等。整体上，这段代码负责将TrueType字体嵌入到PDF文档中，并确保字体可以正确显示。/*
 * << Haru Free PDF Library >> -- hpdf_gstate.c
 *
 * URL: http://libharu.org
 *
 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 * It is provided "as is" without express or implied warranty.
 *
 */

#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf_gstate.h"

HPDF_GState
HPDF_GState_New  (HPDF_MMgr    mmgr,
                  HPDF_GState  current)
{
    HPDF_GState  gstate;

    if (current && current->depth >= HPDF_LIMIT_MAX_GSTATE) {
        HPDF_SetError (mmgr->error, HPDF_EXCEED_GSTATE_LIMIT, 0);

        return NULL;
    }

    gstate = HPDF_GetMem (mmgr, sizeof(HPDF_GState_Rec));
    if (!gstate)
        return NULL;

    if (current) {
        gstate->trans_matrix = current->trans_matrix;
        gstate->line_width =  current->line_width;
        gstate->line_cap =  current->line_cap;
        gstate->line_join =  current->line_join;
        gstate->miter_limit =  current->miter_limit;
        gstate->dash_mode = current->dash_mode;
        gstate->flatness = current->flatness;

        gstate->char_space = current->char_space;
        gstate->word_space = current->word_space;
        gstate->h_scalling = current->h_scalling;
        gstate->text_leading = current->text_leading;
        gstate->rendering_mode = current->rendering_mode;
        gstate->text_rise = current->text_rise;

        gstate->cs_stroke = current->cs_stroke;
        gstate->cs_fill = current->cs_fill;
        gstate->rgb_fill = current->rgb_fill;
        gstate->rgb_stroke = current->rgb_stroke;
        gstate->cmyk_fill = current->cmyk_fill;
        gstate->cmyk_stroke = current->cmyk_stroke;
        gstate->gray_fill = current->gray_fill;
        gstate->gray_stroke = current->gray_stroke;

        gstate->font = current->font;
        gstate->font_size = current->font_size;
        gstate->writing_mode = current->writing_mode;

        gstate->prev = current;
        gstate->depth = current->depth + 1;
    } else {
        HPDF_TransMatrix DEF_MATRIX = {1, 0, 0, 1, 0, 0};
        HPDF_RGBColor DEF_RGB_COLOR = {0, 0, 0};
        HPDF_CMYKColor DEF_CMYK_COLOR = {0, 0, 0, 0};
        HPDF_DashMode DEF_DASH_MODE = {{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, 0.0f, 0.0f};

        gstate->trans_matrix = DEF_MATRIX;
        gstate->line_width = HPDF_DEF_LINEWIDTH;
        gstate->line_cap = HPDF_DEF_LINECAP;
        gstate->line_join = HPDF_DEF_LINEJOIN;
        gstate->miter_limit = HPDF_DEF_MITERLIMIT;
        gstate->dash_mode = DEF_DASH_MODE;
        gstate->flatness = HPDF_DEF_FLATNESS;

        gstate->char_space = HPDF_DEF_CHARSPACE;
        gstate->word_space = HPDF_DEF_WORDSPACE;
        gstate->h_scalling = HPDF_DEF_HSCALING;
        gstate->text_leading = HPDF_DEF_LEADING;
        gstate->rendering_mode = HPDF_DEF_RENDERING_MODE;
        gstate->text_rise = HPDF_DEF_RISE;

        gstate->cs_stroke = HPDF_CS_DEVICE_GRAY;
        gstate->cs_fill = HPDF_CS_DEVICE_GRAY;
        gstate->rgb_fill = DEF_RGB_COLOR;
        gstate->rgb_stroke = DEF_RGB_COLOR;
        gstate->cmyk_fill = DEF_CMYK_COLOR;
        gstate->cmyk_stroke = DEF_CMYK_COLOR;
        gstate->gray_fill = 0;
        gstate->gray_stroke = 0;

        gstate->font = NULL;
        gstate->font_size = 0;
        gstate->writing_mode = HPDF_WMODE_HORIZONTAL;

        gstate->prev = NULL;
        gstate->depth = 1;
    }

    return gstate;
}

HPDF_GState
HPDF_GState_Free  (HPDF_MMgr    mmgr,
                   HPDF_GState  gstate)
{
    HPDF_GState current = NULL;

    if (gstate) {
      current = gstate->prev;
      HPDF_FreeMem (mmgr, gstate);
    }

    return current;
}

